package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Account;
import com.iot08.common.entity.app.Feedback;
import com.iot08.common.response.Response;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Weichao
 * @Date: 2024/09/14/17:37
 * @Description:
 */
@Slf4j
@Service
public class FeedbackService  extends BaseService<Feedback> {

    @Resource
    OperationService operationService;

    /**
     * 列表数据
     *
     * @param page, searchContent, pass
     */
    public Page<Feedback> list(PageVO page, String searchContent, Integer pass) {
        PageableImpl pageable = new PageableImpl();
        pageable.setPage(page);
        page.setSort(Sort.by(Sort.Order.desc("create_time")));

        Criteria criteria = new Criteria();
        if (StringUtils.isNotEmpty(page.getSearchValue())) {
            criteria.and("email").regex(".*?" + page.getSearchValue() + ".*?", "i");
        }
        if (StringUtils.isNotEmpty(searchContent)) {
            criteria.and("content").regex(".*?" + searchContent + ".*?", "i");
        }
        if(pass != null){
            criteria.and("pass").is(pass);
        }
        // delete != 1 的条件
        criteria.and("delete").ne(1);

        Query query = new Query().addCriteria(criteria);
        long count = this.count(query);
        page.setTotalResult((int) count);
        List<Feedback> list = this.find(query.with(pageable));
        Page<Feedback> result = new PageImpl<Feedback>(list, pageable, count);
        return result;
    }
    public Response<Object> changePass(String id, Integer pass, SysUser user, String ip) {
        //权限检查
        if (user.getType() == 1) {
            log.error("({}){} 权限不足 IP:{}",user.getId(), user.getAccount(), ip);
            return Response.failedResponse("权限不足");
        }
        Update update = new Update();
        update.set("pass", pass);
        this.updateById(id, update);
        //操作日志
        operationService.add(user, ip, null, "意见反馈", null,
                "修改", String.format("%s 更新意见反馈：%s", user.getNickname(), pass),
                null, null, null);
        return Response.successResponse();
    }

    public Response<Object> del(String id, SysUser user, String ip) {
        //权限检查
        if (user.getType() == 1) {
            log.error("({}){} 权限不足 IP:{}",user.getId(), user.getAccount(), ip);
            return Response.failedResponse("权限不足");
        }
        Update update = new Update();
        update.set("delete", 1);
        this.updateById(id, update);
        //操作日志
        operationService.add(user, ip, null, "意见反馈", null,
                "删除", String.format("%s 删除意见反馈：%s", user.getNickname(), id),
                null, null, null);
        return Response.successResponse();
//        return Response.failedResponse();
    }
}
